<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>留言管理</cite></a>
        <a><cite>留言数据</cite></a>
    </div>
</div>
<style>
    .layui-input {
        width: 175px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
            <div class="demoTable">
                <!-- 留言内容：
                <div class="layui-inline">
                    <input class="layui-input" name="messageContent" id="messageContent" autocomplete="off">
                </div> -->
                姓名：
                <div class="layui-inline">
                    <input class="layui-input" name="Name" id="Name" autocomplete="off">
                </div>
                联系电话：
                <div class="layui-inline">
                    <input class="layui-input" name="phoneNum" id="phoneNum" autocomplete="off">
                </div>
                企业名称：
                <div class="layui-inline">
                    <input class="layui-input" name="enterpriseName" id="enterpriseName" autocomplete="off">
                </div>
                <!-- 邮箱：
                <div class="layui-inline">
                    <input class="layui-input" name="eMail" id="eMail" autocomplete="off">
                </div> -->
                <!-- 留言时间：
                <div class="layui-inline">
                    <input class="layui-input" name="time" id="time" autocomplete="off">
                </div> -->
                阅读状态：
                <div class="layui-inline">
                    <select name="viewStatus" value="{{ d.params.viewStatus || '' }}" lay-verify="viewStatus" lay-search="">
                        <option value="">直接选择或搜索选择</option>
                        <option value="1">已阅读</option>
                        <option value="2">未阅读</option>
                      </select>
                </div>
                处理状态：
                <div class="layui-inline">
                    <select name="processStatus" value="{{ d.params.processStatus || '' }}" lay-verify="processStatus" lay-search="">
                        <option value="">直接选择或搜索选择</option>
                        <option value="1">已处理</option>
                        <option value="2">未处理</option>
                      </select>
                </div>
                <button class="layui-btn" data-type="reload" lay-submit lay-filter="LAY-user-front-search">搜索</button>
            </div>
        </div>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button type="button" class="layui-btn" id="saveAll" lay-event="saveAll">导出全部数据</button>
            </div>
        </script>
        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table class="layui-hide" id="table-instance" lay-filter="table-instance"></table>
                    <script type="text/html" id="imgTpl">
                        <img style="display: inline-block; width: 50%; height: 100%;" src={ { d.avatar }}>
                    </script>
                    <script type="text/html" id="table-useradmin-webuser">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                    </script>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['layedit', 'admin', 'upload', 'laydate', 'useradmin', 'table', 'form'], function() {
        var $ = layui.$,
            admin = layui.admin,
            setter = layui.setter,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            leyer = layui.leyer,
            upload = layui.upload,
            laydate = layui.laydate,
            layedit = layui.layedit;

        form.render(null, 'layadmin-userfront-formlist');
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data) {
            var field = data.field;
            //执行重载

            table.reload('table-instance', {
                where: field
            });
            // getData('/message/admin/search', field)
        });
        //监听工具栏操作（新增，批量删除等）
        table.on('toolbar(table-instance)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'saveAll':
                    admin.req({
                        url: setter.request.baseUrl + "/message/admin/output",
                        data: '',
                        type: "get",
                        dataType: "json",
                        contentType: "application/json",
                        done: function(res) {
                            // if (res.url != "") {
                            window.location.href = res.url
                                // }

                        },
                    });
                    break;

            };
        });

        // getData()

        // function getData(searchurl, where) {
        // console.log(searchurl)
        // if (searchurl) {
        //     var url = setter.request.baseUrl + searchurl;
        //     var type = "GET"
        //     var where = where;
        // } else {
        //     var url = setter.request.baseUrl + "/message/admin/list";
        //     var type = "GET"
        //     var where = {
        //         typeId: 1
        //     }
        // }
        table.render({
            elem: "#table-instance",
            url: setter.request.baseUrl + "/message/admin/search",
            method: "get",
            // contentType: "application/json",
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板  
            headers: {
                token: layui.data("layuiAdmin").token
            },
            response: {
                statusName: "code",
                statusCode: 0,
                msgName: "msg",
                countName: "count",
                dataName: "list",
            },
            // where: where,
            cols: [
                [{
                    align: 'center',
                    field: "messageId",
                    width: "7%",
                    title: "ID",
                    sort: true
                }, {
                    align: 'center',
                    field: "messageContent",
                    width: "9%",
                    title: "留言内容"
                }, {
                    align: 'center',
                    field: "name",
                    width: "9%",
                    title: "姓名"
                }, {
                    align: 'center',
                    field: "phoneNum",
                    width: "9%",
                    title: "联系电话"
                }, {
                    align: 'center',
                    field: "enterpriseName",
                    width: "15%",
                    title: "企业名称"
                }, {
                    align: 'center',
                    field: "email",
                    width: "9%",
                    title: "邮箱"
                }, {
                    align: 'center',
                    field: "time",
                    width: "8.5%",
                    title: "留言时间",
                    templet: function(d) {
                        console.log(d.releaseTime)
                        var star_releaseTime = layui.util.toDateString(new Date(d.time * 1000).getTime(), "yyyy/MM/dd");
                        return '<div>' + star_releaseTime + '</div>'
                    }
                }, {
                    align: 'center',
                    field: "viewStatus",
                    width: "9.5%",
                    title: "阅读状态",
                    templet: function(res) {
                        var a = '';
                        if (res.viewStatus == 0) {
                            a = '未阅读'
                        } else if (res.viewStatus == 1) {
                            a = '已阅读'
                        }
                        return a
                    }
                }, {
                    align: 'center',
                    field: "processStatus",
                    width: "9.5%",
                    title: "处理状态",
                    templet: function(res) {
                        var a = '';
                        if (res.processStatus == 0) {
                            a = '未处理'
                        } else if (res.processStatus == 1) {
                            a = '已处理'
                        }
                        return a
                    }
                }, {
                    align: 'center',
                    toolbar: "#table-useradmin-webuser" //删除
                }]
            ],
            page: true, //分页
        });
        // }

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        //编辑模块
        table.on("tool(table-instance)", function(obj) {
            var data = obj.data;
            if (obj.event === "edit") {
                admin.popup({
                    title: "编辑留言",
                    area: ["700px", "700px"],
                    success: function(layero, index) {
                        view(this.id)
                            .render("message/contactustable", data)
                            .done(function() {
                                let _data = data;
                                form.render(null, "layuiadmin-form-admin");
                                laydate.render({
                                    elem: '#date'
                                });
                                var releaseTime = layui.util.toDateString(new Date(_data.time * 1000).getTime(), "yyyy-MM-dd");
                                laydate.render({
                                    elem: '#date',
                                    value: releaseTime,
                                    isInitValue: true
                                });
                                console.log(data)
                                form.on("submit(LAY-user-front-submit)", function(data) {
                                    var field = data.field;
                                    var releaseTime = field.time;
                                    field.time = new Date(releaseTime).getTime() / 1000;
                                    field.viewStatus = data.field.viewStatus == 'on' ? '1' : 0
                                    field.processStatus = data.field.processStatus == 'on' ? '1' : 0
                                    editUserHandle(Object.assign(_data, field));
                                    layer.close(index);
                                });
                            });
                    },
                });
            }
        });
        //修改编辑
        function editUserHandle(params) {
            admin.req({
                url: setter.request.baseUrl + "/message/admin/save",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-instance");
                },
            });
        }

    });
</script>